package com.mindbook.vip.gawxchatinfo.controller.user;

import com.alibaba.fastjson.JSONObject;
import com.mindbook.vip.gawxchatinfo.config.shiro.LoginTypeToken;
import com.mindbook.vip.gawxchatinfo.entity.user.UserFront;
import com.mindbook.vip.gawxchatinfo.service.user.UserFrontService;
import com.mindbook.vip.gawxchatinfo.utils.RedisUtils;
import com.mindbook.vip.gawxchatinfo.utils.random.RandomNumberUtils;
import com.mindbook.vip.gawxchatinfo.utils.result.Result;
import com.mindbook.vip.gawxchatinfo.utils.result.ResultEnum;
import com.mindbook.vip.gawxchatinfo.utils.result.ResultUtils;
import com.mindbook.vip.gawxchatinfo.utils.wx.AccessTokenUtils;
import com.mindbook.vip.gawxchatinfo.utils.wx.PhoneNumberUtils;
import com.mindbook.vip.gawxchatinfo.utils.wx.WxUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisConnectionUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Author wxb
 * @Datatime 2022/4/6 12:27
 * @Descript 前端用户管理
 */
@RestController
@RequestMapping("front/user")
@Api(tags = "前端用户")
public class UserFrontController {

    @Autowired
    private UserFrontService userFrontService;

    @Autowired  private WxUtils wxUtils;

    @Autowired private PhoneNumberUtils phoneNumberUtils;

    /**
     * 前台用户注册
     * 如果用户注册成功后，直接登录。
     * 如果用户注册失败，再数据库中已经存在了此用户，则用户同样进行登录。
     * @param code 微信code
     * @return 用户注册是否登录成功
     */
    @PostMapping("register")
    @ApiOperation(value = "用户注册",notes = "使用微信授权用户注册，使用手机号跟登录获取到的code进行用户注册")
    public Result register(
            @ApiParam( required = true,value = "手机号") @RequestParam(name = "phoneNumber") String phoneNumber,
            @ApiParam( required = true,value = "微信登录获取到的code") @RequestParam(name = "code",required = true) String code){
        //1.获取用户的openid
        String openId = wxUtils.getOpenId(code);
        //2.将手机号跟openId进行绑定，并在方法中进行用户登录
        return userFrontService.insert(phoneNumber, openId);
    }

    /**
     * 获取微信绑定的手机号
     * @param code
     * @return
     */
    @GetMapping("/phoneNumber")
    @ApiOperation(value = "获取微信用户手机号",notes = "使用微信授权获取到手机号的code,再通过此方法获取到用户的实际手机号")
    public Result getPhoneNumber(@RequestParam(name = "code")String code){
        String phoneNumber = phoneNumberUtils.getPhoneNumber(code);
        return ResultUtils.genSuccessResult(phoneNumber);
    }

    /**
     * 获取前段用户的个人信息
     * @return
     */
    @GetMapping("/info")
    public Result getUserInfo(){
        Object principal = SecurityUtils.getSubject().getPrincipal();
        return ResultUtils.genSuccessResult(principal);

    }

    /**
     * 判断当前用户是否登录
     * bug:当前可以判断所有用户是否已经登录。而不是简单的断定前段用户是否登录
     * @return
     */
    @GetMapping("/logging")
    @ApiOperation(value = "获取当前用户是否正在登录中")
    public Result isLoggingIn(){
        Subject subject = SecurityUtils.getSubject();
        boolean authenticated = subject.isAuthenticated();
        if(authenticated){
            return ResultUtils.genSuccessResult("登录中");
        }
        return ResultUtils.genErrorResult(ResultEnum.SHIRO_CURRENT_USER_NOT_LOGIN);
    }




}
